import { RouteRecordRaw } from 'vue-router'
import router from '.'

const modules = import.meta.glob('./modules/*.ts', { eager: true })

export const addAppRoutes = (_routes: RouteRecordRaw[]) => {
  _routes.forEach((item) => {
    router.addRoute({ ...item })
  })
}

let clientRoutes: RouteRecordRaw[] = []

Object.keys(modules).forEach((key) => {
  const defaultModule = (modules as any)[key].default
  if (!defaultModule) return
  const moduleList = Array.isArray(defaultModule)
    ? [...defaultModule]
    : [defaultModule]
  clientRoutes.push(...moduleList)
})

clientRoutes = clientRoutes.sort((a: any, b: any) => a.meta.sort - b.meta.sort)

export default clientRoutes
